package com.example.wuyou.config;

import com.example.wuyou.aop.TokenInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册TestInterceptor拦截器
        registry.addInterceptor(tokenInterceptor())//添加不拦截的请求路径
                .excludePathPatterns("/user/login","/user/getOpenId","/user/wxUserLogin","/swagger-ui.html/**","/swagger-resources/**")
                .addPathPatterns("/**");						//添加需要拦截的路径
    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 匹配所有路径
                .allowedOrigins("http://localhost:8081", "http://localhost:8080") // 允许的前端地址
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的请求方法
                .allowCredentials(true) // 允许发送 Cookie
                .allowedHeaders("*") // 允许的请求头
                .exposedHeaders("Authorization") // 暴露的响应头
                .maxAge(3600); // 预检请求的缓存时间（秒）
    }
    @Bean
    public TokenInterceptor tokenInterceptor(){
        return new TokenInterceptor();
    }
}
